import { 
  SET_SIGN_FRONT, 
  SET_SIGN_BACK, 
  SEND, 
  SEND_SUCCESS,
  SEND_FAIL,
  CHANGE_PHONE_SELECT_TYPES,
  GET_SIGN_DATA_SUCCESS,
  IMPORT_FILE,
  IMPORT_FILE_SUCCESS,
  IMPORT_FILE_FAIL,
  SET_PHONES,
} from '@/actions/send'
import { SET_SIGN, SET_MESSAGE_CONTENT } from '../actions/send';

const initState = {
  sign: '', // 签名
  signData: [], // 签名枚举
  signType: 1, //前置为 1 后置为 0
  messageContent: '', // 短信内容
  phones: null, // 号码池
  fileData: {}, // 导入文件数据
  fileName: '',
  phoneSelectTypes: [], // 运营商选择
  importButtonLoading: false,
  isSendButtonLoading: false,
}

const sendReducer = (state = initState, action) => {
  switch (action.type) {
    case SET_SIGN_FRONT:
      return { ...state, signType: 1 }
      break;

    case SET_SIGN_BACK:
      return { ...state, signType: 0 }
      break;

    case GET_SIGN_DATA_SUCCESS:
      const { signData } = action
      // 找出默认签名
      const { sign } = signData.find(({ defauls }) => defauls === 1) || {} 
      return { ...state, signData: action.signData, sign }
      break;

    case IMPORT_FILE:
      return { ...state, importButtonLoading: true }
      break;

    case IMPORT_FILE_SUCCESS:
      return { ...state, importButtonLoading: false, fileData: action.fileData, fileName: action.fileName }
      break;

    case IMPORT_FILE_FAIL:
      return { ...state, importButtonLoading: false }
      break;

    case SET_PHONES:
      return { ...state, phones: action.phones }
      break;

    case SET_SIGN:
      return { ...state, sign: action.sign }
      break;

    case SET_MESSAGE_CONTENT:
      return { ...state, messageContent: action.messageContent }
      break;

    case SEND:
      return { ...state, isSendButtonLoading: true }
      break

    case SEND_SUCCESS:
      return { ...state, signType: 1, messageContent: '', phones: null, fileData: {}, isSendButtonLoading: false, fileName: '' }
      break;

    case SEND_FAIL:
      return { ...state, isSendButtonLoading: false, }
      break

    case CHANGE_PHONE_SELECT_TYPES:
      return { ...state, phoneSelectTypes: action.phoneSelectTypes }
  
    default:
      return state
      break;
  }
}

export default sendReducer